package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.RoomOrder;
import com.ruoyi.system.service.IRoomOrderService;
import com.ruoyi.system.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 客房订单Controller
 *
 * @author 强盛酒店
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/system/roomOrder")
public class RoomOrderController extends BaseController {
    @Autowired
    private IRoomOrderService roomOrderService;
    @Autowired
    private IRoomService roomService;

    /**
     * 查询客房订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RoomOrder roomOrder) {
        startPage();
        List<RoomOrder> list = roomOrderService.selectRoomOrderList(roomOrder);
        if (!CollectionUtils.isEmpty(list)){
            for (RoomOrder order : list) {
                Room room = roomService.selectRoomById(order.getRoomId());
                order.setRoomName(room.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取客房订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(roomOrderService.selectRoomOrderById(id));
    }

    /**
     * 获取客房订单详细信息
     */
    @GetMapping(value = "/my")
    public TableDataInfo getMyOrder() {
        startPage();
        List<RoomOrder> orderList = roomOrderService.getMyOrder();
        return getDataTable(orderList);
    }

    /**
     * 新增客房订单
     */
    @PostMapping
    public AjaxResult add(@RequestBody RoomOrder roomOrder) throws ParseException {
        return toAjax(roomOrderService.insertRoomOrder(roomOrder));
    }

    /**
     * 新增客房订单
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody RoomOrder roomOrder) throws ParseException {
        return toAjax(roomOrderService.submitRoomOrder(roomOrder));
    }

    /**
     * 修改客房订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody RoomOrder roomOrder) throws ParseException {
        return toAjax(roomOrderService.updateRoomOrder(roomOrder));
    }

    /**
     * 取消客房订单
     */
    @PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        return toAjax(roomOrderService.cancel(id));
    }

    @PutMapping("/audit/{id}")
    public AjaxResult audit(@PathVariable("id") Long id) throws ParseException {
        return toAjax(roomOrderService.auditRoomOrder(id));
    }

    /**
     * 删除客房订单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomOrderService.deleteRoomOrderByIds(ids));
    }


}
